﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HttpRequester;
using mshtml;
using Common.Constants;
using BotvaBot.BusinessLayer.Enums;
using BotvaBot.Configuration;
using BotvaBot.BusinessLayer.RequestChainItems.Factory;

namespace BotvaBot.RequestChainItems
{
    [ChainItem]
    public class ActivateKulonChainItem : BotvaLogedInChainItemBase
    {
        public Kulon KulonType { get; set; }

        protected override bool IsContextValidInternal()
        {
            return base.IsContextValidInternal();
        }

        protected override void PrepareRequestInternal()
        {
            var personaj = Config.Custom.PersonajSet.Items.Where(p => p.Email == Name).FirstOrDefault();

            int kulonId = -1;
            var kulon = personaj.KulonSet.Items.Where(k => k.Type == KulonType).FirstOrDefault();
            if (kulon == null)
                return;

            kulonId = kulon.ID;

            Url = "http://g3.botva.ru/index.php";
            Method = HttpMethod.GET;


            string content = Chain.LastContent;

            //?deactivate=7001339
            string lookingFor = @"?activate=" + kulonId.ToString();
            int start = content.IndexOf(lookingFor);
            if (start < 0)
                return;
            
            //start += @"<a class=""cmd_all cmd_mini_sl cmd_amini_sl "" href=""".Length;
            int end = content.IndexOf(@"""", start);
            if(end < 0)
                return;

            string queryString = content.Substring(start, end - start);

            Url += queryString.Replace("&amp;", "&");

            //<a class="cmd_all cmd_mini_sl cmd_amini_sl " href="?deactivate=6095978&show=weapons&k=10030">СНЯТЬ</a>
        }

        public override bool IsResponseValidInternal()
        {
            //string content = Requester.PageContent;
            //content = content.Replace(" ", string.Empty)
            //                .Replace("'", "\"")
            //                .ToUpper();

            //string searchedStr = "<input type='submit' class='cmd_all cmd_medium_sl cmd_amedium_sl ' value='ВХОД' >";
            //searchedStr = searchedStr.Replace(" ", string.Empty)
            //                .Replace("'", "\"")
            //                .ToUpper();

            //return content.Contains(searchedStr);

            return true;
        }
    }
}
